package com.legsem.jrhosync.ws.rs;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.legsem.jhosync.dto.ClientLogin;

public class ClientCreateResourceITCase extends AbstractResourceITCase {

    public void testShouldNotCreateANewClientIfUserDidNotLogin()
            throws Exception {

        try {
            getJSONRequest("/clientcreate");
            fail();
        } catch (Exception e) {
            assertTrue(e.getMessage().startsWith(
                    "Request returned:500 message:"));
        }
    }

    public void testShouldCreateANewClientAfterSuccessfulLogin()
            throws Exception {
        Pattern pattern1 = Pattern
                .compile("\"Customer\":\\{\"poll_interval\":3\\,\"partition_type\":\"app\"}");
        Pattern pattern2 = Pattern
                .compile("\"Appointment\":\\{\"poll_interval\":300\\}");
        Pattern pattern3 = Pattern
                .compile("\"client\":\\{\"client_id\":\"\\w{32}\"\\}");

        ClientLogin clientLogin = new ClientLogin();
        clientLogin.setLogin("tartempion");
        clientLogin.setPassword("");
        postJSONRequest("/clientlogin", clientLogin);

        byte[] response = getJSONRequest("/clientcreate");
        String readableResponse = new String(response, "UTF-8");

        Matcher matcher = pattern1.matcher(readableResponse);
        assertTrue(matcher.find());
        matcher = pattern2.matcher(readableResponse);
        assertTrue(matcher.find());
        matcher = pattern3.matcher(readableResponse);
        assertTrue(matcher.find());
    }

}
